package com.cgah

import grails.test.*
import com.cgah.Customer
import com.cgah.Listing
import com.cgah.Bid

class BidTests extends GrailsUnitTestCase {
    Listing theListing;
    Customer seller;
    Customer bidder;
    protected void setUp() {
        super.setUp();
        mockDomain(Bid);
        mockDomain(Listing);
        mockDomain(Customer);
        seller = new Customer( username: "sellerdoe", emailAddress: "asdf@asdf.com", createdDate: new Date(), password: "12312312");
        theListing = new Listing(createdDateTime: new Date(), description: "asdf", seller:seller, startingBidPrice: 11.0f, name: "test", endDateTime: (new Date())+1);
        bidder = new Customer(username: "bidderdoe", emailAddress: "qweqwe@qweqwe.com", createdDate: new Date(), password: "12312312", enabled:true);
       }

    protected void tearDown() {
        super.tearDown()
    }

    //B-6: When a Listing is saved, any new Bids added for the listing must be saved (integration test)
    // why are we testing one to many save??????????????????????
    // if this doesn't work... well you've got more problems.......


    void testCascadeSaveFromListing() {
//
        Bid b1 = new  Bid(nextMinBid: 12.5f, amount: 12.0f, dateTime: new Date() + 1, listing: theListing, customer:bidder);
        Bid b2 = new  Bid(nextMinBid: 13.5f, amount: 13.0f, dateTime: new Date() + 2, listing: theListing, customer:bidder);
        Bid b3 = new  Bid(nextMinBid: 14.5f, amount: 14.0f, dateTime: new Date() + 3, listing: theListing, customer:bidder);


        theListing.addToBids(b1);
        theListing.addToBids(b2);
        theListing.addToBids(b3);

        // put in db
        theListing.save(flush:true);

        b1.save()
        // pull back out.
        def bidListCheck = Listing.list();
        assertEquals(bidListCheck.get(0).bids.size(), 3);

    }

    //Done - B-5: The Bid amount must be at least .50 higher than the previous Bid for the same item (integration test)
    void testNewBidGreaterThanPrevBid() {
        Bid b1 = new  Bid(amount: 12.0f, dateTime: new Date() + 1, listing: theListing, customer:bidder);
        theListing.addToBids(b1);
        b1.save(flush:true);
        Bid b2 = new  Bid(amount: 11.0f, dateTime: new Date() + 2, listing: theListing, customer:bidder);
        theListing.addToBids(b2);
       assertFalse("failed", b2.validate());
    }

    //Done - B-3: Bids are required to have a bidder (Customer) (integration test)
    void testBidRequiresBidder() {
        Bid b1 = new  Bid(amount: 12.0f, dateTime: new Date() + 1, listing: theListing, customer:bidder);
        assertTrue("vailid bid", b1.validate());

        b1.customer = null;
        assertFalse("valid bid", b1.validate());
    }

    //    Done - B-2: Bids are required to be for a Listing (integration test)
    void testBidRequiredForListing() {
        Bid b1 = new  Bid(amount: 12.0f, dateTime: new Date() + 1, listing: theListing, customer:bidder);
        assertTrue("vailid bid", b1.validate());

        b1.listing = null;
        assertFalse("valid bid", b1.validate());
    }

    //Done - B-1: Bids have the following required fields: amount and date/time of bid (integration test)
    void testFieldsExist() {
        def c = new Bid(amount: 1.0, dateTime: new Date());
        assertNotNull(c);
    }
}
